// This file is part of OpenTSDB.
// Copyright (C) 2017 TeleDB Development Group.
//
// This program is free software: you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 2.1 of the License, or (at your
// option) any later version.  This program is distributed in the hope that it
// will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
// General Public License for more details.  You should have received a copy
// of the GNU Lesser General Public License along with this program.  If not,
// see <http://www.gnu.org/licenses/>.
package net.opentsdb.core.tsuid;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TestHbaseClient {

    private static final Logger LOG = LoggerFactory.getLogger(TestHbaseClient.class);

    private String tableName;
    private Configuration config;

    @Before
    public void before(){
        tableName = "tsdb";
        config = HBaseConfiguration.create();
        config.set("hbase.zookeeper.quorum","127.0.0.1:2181");
        config.set("hbase.zookeeper.basedir","/hbase");
    }

    @Test
    public void TestConnect() throws Exception{
        LOG.debug("-------------------------start check hbase table-----------------------------------");
        try (Connection connection = ConnectionFactory.createConnection(config);
             Admin admin = connection.getAdmin()){
            LOG.debug("table is exists: {}",admin.tableExists(TableName.valueOf(tableName)));
        }
    }

    @Test
    public void TestPutGetAndDelData(){
        String key = "metric";
        String family = "t";
        String qualifer = "info";
        String value = "001";

        try (Connection connection = ConnectionFactory.createConnection(config)){
            LOG.debug("------------------------start get table ------------------");
            Table table = connection.getTable(TableName.valueOf(tableName));

            LOG.debug("-------------------------start put data-----------------------------------");
            Put put = new Put(key.getBytes());
            put.addColumn(family.getBytes(), qualifer.getBytes(), value.getBytes());
            table.put(put);

            LOG.debug("-------------------------start get data-----------------------------------");
            Get get = new Get(key.getBytes());
            Result result = table.get(get);
            LOG.debug("result:{} ,get result:{}",result.isEmpty(),
                    new String(result.getValue(family.getBytes(), qualifer.getBytes())));

            LOG.debug("-------------------------start delete data-----------------------------------");
            Delete del = new Delete(key.getBytes());
            table.delete(del);

            LOG.debug("-------------------------second get data-----------------------------------");
            Get get2 = new Get(key.getBytes());
            Result result2 = table.get(get2);
            LOG.debug("result is empty: {}", result2.isEmpty());
        }catch (Exception e){
            LOG.error("get table error:",e);
        }
    }
}
